<template>
  <!-- 登录 -->
  <div class="box">
    <div class="conter">
      <div class="logo">
        <div class="tb_logo"></div>
      </div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          placeholder="用户名"
          :rules="[
						{ validator: verifyUserName, message: '填写正确的电话号码' },
					]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          placeholder="请输入登录密码"
          :rules="[{ validator: verifyPassword, message: '请填写6-18密码' }]"
        />
        <div class="a">
          <a href="#" class="a1">短信验证码登录</a>
          <a href="#" class="a2" @click="reg">免费注册</a>
        </div>
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit">登录</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import indexApi from "@/api/indexApi.js"; //引入接口
import { setToken, setUser } from "@/utils/auth"; //引入cookie设置的相关方法
export default {
  data() {
    return {
      username: "",
      password: ""
    };
  },

  components: {},

  methods: {
    async onSubmit(values) {
      try {
        let psw = this.$md5(this.password);
        let p = await indexApi.getlogin(this.username, psw);
        console.log("123");
        if (p.data.flag) {
          this.$notify({ type: "success", message: "恭喜你登录成功" });
          let userinf = {
            username: this.username,
            uid: p.data.uid
          };
          setToken(p.data.token);
          setUser(JSON.stringify(userinf));
          this.$router.push({
            name: "index"
          });
        } else {
          this.$notify({ type: "warning", message: "密码错误" });
        }
      } catch (err) {}
    },
    verifyUserName(username) {
      return /^1[3|4|5|7|8]\d{9}$/.test(username);
    },
    verifyPassword(password) {
      return /^\w{6,18}$/.test(password);
    },

    reg() {
      this.$router.push(this.reg);
    }
  },
  created() {
    if (this.$route.query) {
      this.username = this.$route.query.name;
    }
  }
};
</script>

<style scoped>
body {
  font-size: 16px;
}

.tb_logo {
  background: url(../assets/images/index/logo.png) no-repeat;
  background-size: contain;
  border-radius: 100%;
  width: 80px;
  height: 80px;
  margin: 40px auto 0;
}

.conter {
  padding: 10px 5px;
}

.conter > h2 {
  margin-top: 20px;
  font-size: 30px;
  color: #333;
}
.conter > span {
  line-height: 40px;
  margin-top: 12px;
  font-size: 16px;
  color: #999;
}
input::-webkit-input-placeholder {
  color: #000;
}
.van-field {
  margin: 20px 0;
}
.van-form .van-cell::after {
  border-bottom: 1px solid #ff5000;
  opacity: 1;
}
.van-form {
  margin-top: 30px;
}
.van-button {
  margin: 75px 0 0 0;
  background-image: -webkit-gradient(
    linear,
    left top,
    right top,
    from(#f90),
    to(#ff5000)
  );
  background-image: -webkit-linear-gradient(left, #f90, #ff5000);
  background-image: -o-linear-gradient(left, #f90, #ff5000);
  background-image: linear-gradient(90deg, #f90, #ff5000);
  border: none;
  border-radius: 24px;
  height: 45px;
}

/* a1 */
.a {
  padding: 0 15px;
  font-size: 14px;
  margin-bottom: 30px;
}
.a1 {
  float: left;
  color: #555;
}
.a2 {
  color: #555;
  float: right;
}
</style>
